<?php
namespace app\agenttest\controller;
use app\common\model\Agent;
use app\common\model\LoginUsers;
use app\common\model\WxOpen;


class Agents extends AgenttestBase
{

    /*
     * 代理商详细信息
     * */
    public function agentinfo(){
        $Agent = new Agent();
        $LoginUsers = new LoginUsers();
        $map['id'] = $this->get_agent_id();
        $agentinfo = [];
        $agentinfo = $Agent->where($map)->find();

        $user = $LoginUsers->where('id',$agentinfo['uid'])->find();
        $agentid = $this->get_agent_id();
        $agentinfo['ticketurl'] = "https://" . $agentinfo['open_url'] . "/index.php/Wxauth/ticket/id/" . $agentid;
        $agentinfo['msgurl'] = "https://" . $agentinfo['open_url'] . "/api.php/id/" . $agentid . "/appid/\$APPID$";
        $this->assign('agentinfo',$agentinfo);
        $this->assign('user', $user);
        return $this->fetch();
    }

    /*
     * 代理商信息修改
     * */
    public function agentxg()
    {
        $id = $this->get_agent_id();

        $request = request();
        if (Request()->isPost()) {
            $arr = $request->post();
            $LoginUsers = new LoginUsers();
            $Agent = new Agent();
            $data = $arr;
            $data['name']=trim($arr['name']);
            $data['contacts']=trim($arr['contacts']);
            $data['tel']=trim($arr['tel']);
            $data['address']=trim($arr['address']);
            $add = $Agent->allowField(true)->save($data,['id'=>$id]);
            if ($add!== false) {
                $uid = $Agent->where('id',$id)->value('uid');
                $datas['xingming'] = trim($arr['name']);
                $datas['tel'] = trim($arr['tel']);
                $LoginUsers->allowField(true)->validate('LoginUsers.edit')->save($datas,['id'=>$uid]);
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }
        } else {
            $Agent = new Agent();
            $map['id'] = $id;
            $agentinfo = $Agent->where($map)->Relation('LoginUsers')->find();
            $this->assign('agentinfo',$agentinfo);
            return $this->fetch();
        }
    }

    /*
     * 判断是否代理
     * 参数
     * $agentname  代理商名称
     * */
    public function is_agent()
    {
        if (Request()->isAjax()) {
            $request = request();
            $Agent = new Agent();
            $arr = $request->post('agentname');
            $map['name'] = trim($arr);
            $agent = $Agent->where($map)->count();
            if ($agent > 0) {
                $data = 0;
            } else {
                $data = 1;
            }
            return json($data);
        }


    }

    //重置密码
    public function resetpassword(){
        if (Request()->isAjax()){
            $arr = request()->post();
            $LoginUsers = new LoginUsers();
            $password = $this->getrandmopassword(8);
            $data['password'] = md5($password);
            $res = $LoginUsers->allowField(true)->save($data,array('username',$arr['username']));
            if ($res){
                $rarr['code'] = 0;
                $rarr['message'] = '重置成功';
                $rarr['new_password'] = $data['password'];
            }else{
                $rarr['code'] = 9001;
                $rarr['message'] = '重置失败';
            }
            return json();
        }
    }

    //生成随机密码
    private function getrandmopassword($len, $chars=null){
        if (is_null($chars)){
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        }
        mt_srand(10000000*(double)microtime());
        for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++){
            $str .= $chars[mt_rand(0, $lc)];
        }
        return $str;
    }

    /*
     * 判断是否会员
     * 参数
     * $username  用户名称
     * */
    public function is_user()
    {
        if (Request()->isAjax()) {
            $request = request();
            $arr = $request->post();
            $LoginUsers = new LoginUsers();
            $map['username'] = trim($arr['username']);
            $user = $LoginUsers->where($map)->count();
            if ($user > 0) {
                $data = 0;
            } else {
                $data = 1;
            }
            return json($data);
        }

    }

    public function generate_rand()
    {
        if (request()->isAjax()) {
            $num=request()->post('num');

            return generate_rand($num);
        }
    }
}
